export default defineContentScript({
  matches: ["https://jira.cjdropshipping.cn/*"],
  main() {
    window.onload = function () {
      const server = createDOMObserver({
        target: "body",
        subtree: false,
        callback: (mutations) => {
          mutations.forEach((mutation) => {
            if (
              mutation.type === "childList" &&
              mutation.addedNodes.length > 0 &&
              mutation.addedNodes[0].id === "create-subtask-dialog"
            ) {
              var dialog = document.querySelector("#create-subtask-dialog");
              if (dialog) {
                setTimeout(() => {
                  var summaryInput = document.querySelector(
                    "#fixVersions-textarea"
                  ) as HTMLInputElement;
                  var summary = document.querySelector(
                    "#summary"
                  ) as HTMLInputElement;
                  if (!summaryInput.innerText) {
                    var btn = document.querySelector(
                      "#assign-to-me-trigger"
                    ) as HTMLElement;
                    btn?.click();
                    var summaryVal = document.querySelector(
                      "#summary-val"
                    ) as HTMLInputElement;
                    summaryInput.value = summaryVal.innerText;
                    summaryInput.focus();
                    summaryInput.click();
                    document.querySelector(".aui-list-item.active")?.click();
                    summary.focus();
                  }
                }, 1000);
              }
            }
          });
        },
      });
    };
  },
});
